<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" placeholder="请输入内容" class="search-main" />
      <el-button
        type="primary"
        @click="doSearch"
      >查询</el-button>
      <!-- <el-button v-auth-btn="'park:building:add_edit'" type="primary" @click="$router.push('/enterpriseAdd')">添加企业</el-button>
      <el-button v-auth-btn="'park:building:add_edit'">删除企业</el-button> -->
    </div>
    <!-- 添加楼宇弹框 -->
    <!-- 添加楼宇弹框 -->
    <el-dialog
      :title="isId?'编辑':'添加'"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" placeholder="请输入楼宇名称" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input
              v-model="addForm.floors"
              placeholder="请输入楼宇层数"
            />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input
              v-model="addForm.area"
              placeholder="请输入在管面积"
            />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input
              v-model="addForm.propertyFeePrice"
              placeholder="请输入物业费"
            />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="confirmAdd"
        >确 定</el-button>
      </template>

    </el-dialog>

    <!-- 添加按钮 -->
    <div class="create-container">
      <el-button
        type="primary"
        @click="addBuliding"
      >添加楼宇</el-button>
      <el-button>导出表格</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        ref="addForm"
        style="width: 100%"
        :data="buildingList"
      >
        <el-table-column prop="id" label="序号" />
        <el-table-column prop="name" label="楼宇名称" width="180" />
        <el-table-column prop="floors" label="层数" />
        <el-table-column
          prop="area"
          label="在管面积(m²)"
          width="120"
        />
        <el-table-column
          prop="propertyFeePrice"
          label="物业费(元/m²)"
          width="120"
        />
        <el-table-column label="状态" prop="status">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" :disabled="scope.row.status===0" @click="editBuliding(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" :disabled="scope.row.status===1" @click="delBuliding(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @current-change="pageChange"
      />
    </div>
  </div>
</template>

<script>
import { getBuildingListAPI, addBulidingAPI, delBuildingListAPI, editBulidingAPI } from '@/api/building'
// import { utils, writeFileXLSX } from 'xlsx'
export default {
  name: 'Building',
  data() {
    return {
      buildingList: [],
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      addForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      addFormRules: {
        name: [{ required: true, message: '请输入楼宇名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        floors: [{ required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [{ required: true, message: '请输入楼宇面积', trigger: 'blur' }],
        propertyFeePrice: [{ required: true, message: '请输入楼宇物业费', trigger: 'blur' }]
      },
      isId: null
    }
  },
  mounted() {
    this.getBuildingList()
  },
  methods: {
    async getBuildingList() {
      const res = await getBuildingListAPI(this.params)
      this.buildingList = res.data.rows
      this.total = res.data.total
      console.log(res)
    },
    formatStatus(status) {
      const statusMap = {
        0: '租赁中',
        1: '闲置中'
      }
      return statusMap[status]
    },
    // 切换分页
    pageChange(page) {
      this.params.page = page
      this.getBuildingList()
    },
    // 搜索
    doSearch() {
      this.params.page = 1
      this.getBuildingList()
    },
    // 添加
    addBuliding() {
      this.isId = null
      this.dialogVisible = true
      // this.addForm = {}
      // this.$refs.addForm.resetFields()
    },
    closeDialog() {
      this.dialogVisible = false
      this.addForm = {}
      this.$refs.addForm.resetFields()
    },
    // 表单校验
    confirmAdd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.isId) {
            await editBulidingAPI(this.addForm)
          } else {
            // console.log('添加')
            await addBulidingAPI(this.addForm)
          }
          // 重新获取列表
          this.getBuildingList()
          // 关闭弹窗
          this.dialogVisible = false
        }
      })
    },
    // 删除楼宇
    async delBuliding(id) {
      this.$confirm('确认删除当前楼宇吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delBuildingListAPI(id)
        this.getBuildingList()
      }).catch(() => {

      })
    },
    // 编辑楼宇
    editBuliding(row) {
      this.isId = row.id
      // console.log(this.isId)
      this.dialogVisible = true
      const { id, area, floors, name, propertyFeePrice } = row
      this.addForm = { id, area, floors, name, propertyFeePrice }
      // console.log(row)
    }
    // 导出表格

  }
}
</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-label {
    width: 100px;
  }
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.page-container{ padding:4px 0px; text-align: right; }

</style>
